<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*
      这题和组合一样，只不过不需要<=n，每次都是<=9
      */
      var combinationSum3 = function (k, n) {
        let res = []
        function backTracking(path, sum, startIndex) {
          //剪枝
          if (sum > n) return
          if (path.length == k && sum == n) {
            return res.push([...path])
          }
          for (let i = startIndex; i <= 9; i++) {
            path.push(i)
            backTracking(path, sum + i, i + 1)
            path.pop()
          }
        }
        backTracking([], 0, 1)
        return res
      }
    </script>
  </body>
</html>
